<template>
  <div class="login-wrap">
    <el-form class="login-container">
      <h1 class="title">用户登录</h1>
      <el-form-item>
        <el-input
          type="text"
          placeholder="用户账号"
          v-model="username"
          auto-complete="off"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-input
          type="password"
          placeholder="密码"
          v-model="password"
          auto-complete="off"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="doLogin" style="width: 100%"
          >登录</el-button
        >
        <el-row style="text-align: center; margin-top: 10px">
          <el-link type="primary" @click="toRegister">用户注册</el-link>
          <el-link type="primary">忘记密码</el-link>
        </el-row>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { loginIn } from '../api/index'
import { ElMessage, ElMessageBox } from 'element-plus'

export default {
  name: 'LoginView',
  data: function () {
    return {
      username: '1',
      password: '1',
    }
  },
  methods: {
    doLogin: function () {
      var user = {
        username: this.username,
        password: this.password,
      }
      //校验用户输入.....

      loginIn(user)
        .then((res) => {
          ElMessage({
            message: res.msg,
            type: 'success',
          })
          const username = res.map.userInfo.username
          const phone = res.map.userInfo.phone
          const sex = res.map.userInfo.sex
          const userId = res.map.userInfo.uid
          const realName = res.map.userInfo.realname
          const address = res.map.userInfo.address
          const email = res.map.userInfo.email
          const idCard = res.map.userInfo.idcard
          const age = res.map.userInfo.age
          const type = res.map.userInfo.type

          if (res.code == 1) {
            // this.$store.commit('savePassword', username)
            this.$store.commit('saveRealname', realName)
            this.$store.commit('saveSex', username)
            this.$store.commit('saveAddress', address)
            this.$store.commit('saveIdCard', idCard)
            this.$store.commit('saveEmail', email)
            this.$store.commit('saveAge', age)
            this.$store.commit('savePhone', phone)
            this.$store.commit('saveSex', sex)
            this.$store.commit('saveType', type)
            this.$store.commit('saveUsername', username)
            this.$store.commit('saveUserId', userId)
            localStorage.setItem('Flag', 'isLogin')
            this.$store.commit('saveIsLogin', true) //设置vuex中的登陆状态为已经登陆
            this.$router.push('/')
          }
        })
        .catch()
    },
    toRegister: function () {
      this.$router.push('/Register')
    },
  },
}
</script>

<style scoped>
.login-wrap {
  box-sizing: border-box;
  width: 100%;
  height: 100%;
  padding-top: 10%;
  background-image: url();
  /* background-color: #112346; */
  background-repeat: no-repeat;
  background-position: center right;
  background-size: 100%;
}

.login-container {
  border-radius: 10px;
  margin: 0px auto;
  width: 350px;
  padding: 30px 35px 15px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  text-align: left;
  box-shadow: 0 0 20px 2px rgba(0, 0, 0, 0.1);
}

.title {
  margin: 0px auto 40px auto;
  text-align: center;
  color: #505458;
}
</style>
